package com.cyrene.stock.service.impl;

import com.cyrene.stock.cache.Cache;
import com.cyrene.stock.constant.RedisConstants;
import com.cyrene.stock.response.ResponseResult;
import com.cyrene.stock.service.UserService;
import com.cyrene.stock.util.IdWorkers;
import com.cyrene.stock.util.RedisKeyUtil;
import com.cyrene.stock.vo.CaptchaVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:Shuna
 * @Date: 2025/11/07 21:00
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private Cache cache;

    @Autowired
    private IdWorkers idWorkers;

    @Override
    public CaptchaVO generateCaptcha() {
        String captcha = RandomStringUtils.randomNumeric(4);
        // 2. 将验证码存入缓存
        // 生成key的后缀
        String keySuffix = idWorkers.nextStrId();
        // 生成key
        String redisKey = RedisKeyUtil.generateKey(RedisConstants.CAPTCHA_PREFIX, keySuffix);
        // 存入缓存，并且设置过期时间
        cache.set(redisKey, captcha, RedisConstants.CAPTCHA_EXPIRE);
        // 3. 数据组装返回
        return  CaptchaVO.builder()
                        .code(captcha)
                        .rkey(redisKey)
                        .build();
    }

}
